﻿using System;
using System.Collections;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace IOFileTest
{
    public class MittausData
    {
        
        private string kello;
        public string Kello { 
            get {return kello;} 
            set {kello = value;} 
        }

        public string Mittaus { get; set; }
        public  MittausData(string kello, string data)
        {
            this.kello = kello;
            this.Mittaus = data;
        }
        public override string ToString()
        {
            return this.kello + " --> " + this.Mittaus;
        }
        public string DataMuoto
        {
            get { return kello + " --> " + this.Mittaus; }
        }
    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ArrayList mitatut;
        public MainWindow()
        {
            InitializeComponent();
            IniMyStuff();
        }
           private void IniMyStuff()
           {
               MittausData md = new MittausData("10:27", "100");
               tbToday.Text = DateTime.Today.ToShortDateString();
               mitatut = new ArrayList();
               mitatut.Add(md);
               MittausData md1 = new MittausData("16:17", "124");
               mitatut.Add(md1);
               ApplyChanges();
           }
           private void ApplyChanges()
           {
               lbData.ItemsSource = null;
               lbData.ItemsSource = mitatut;
           }

           private void btnNew_Click(object sender, RoutedEventArgs e)
           {
               if (txtClock.Text.Length * txtData.Text.Length > 0)
               {
                   MittausData md = new MittausData(txtClock.Text, txtData.Text);
                   mitatut.Add(md);
                   ApplyChanges();
               }
           }

           private void btnSave_Click(object sender, RoutedEventArgs e)
           {
               try
               {
                   if (System.IO.File.Exists(txtFilename.Text))
                   {
                       using (StreamWriter sw = File.AppendText(txtFilename.Text)) 
                       {
                           foreach (MittausData md in mitatut)
                           {
                               sw.WriteLine(md.DataMuoto);
                           }
                       }
                   }
                   else
                   {
                       using (StreamWriter sw = File.CreateText(txtFilename.Text))
                       {
                           foreach (MittausData md in mitatut)
                           {
                               sw.WriteLine(md.DataMuoto);
                           }
                       }
                   }
                   MessageBox.Show("Tallennettu!");
               }
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message);              
               }
           }
    }

 
}
